﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using YArchitech.Revit.Electric.Common;
using HYCodeBase.WinForm;

namespace ElectricDim
{
	public partial class DlgWireDim : DlgModelLess
	{
		public DlgWireDim(UIApplication uiApp, WindowHandle handel) : base(handel)
		{
			this.mApp = uiApp;
			this.InitializeComponent();
		}

		public static DlgWireDim GetInstance(UIApplication app, WindowHandle handel)
		{
			if (DlgWireDim.dll == null || DlgWireDim.dll.IsDisposed)
			{
				DlgWireDim.dll = new DlgWireDim(app, handel);
			}
			return DlgWireDim.dll;
		}

		private void DlgWireDim_Load(object sender, EventArgs e)
		{
			foreach (DbFamilyObject item in ElectricMark.WireMark(this.mApp.ActiveUIDocument.Document))
			{
				this.cbxFamily.Items.Add(item);
			}
			if (this.cbxFamily.Items.Count > 0)
			{
				this.cbxFamily.SelectedIndex = 0;
			}
		}

		public LeaderStyle GetDimStyle()
		{
			if (this.rbNullLeader.Checked)
			{
				return LeaderStyle.None;
			}
			if (this.rbHasLeader.Checked)
			{
				return LeaderStyle.HasLeader;
			}
			return LeaderStyle.Orthogonal;
		}

		public PointStyle GetDimLinkStyle()
		{
			if (this.rbNull.Checked)
			{
				return PointStyle.None;
			}
			if (this.rbLine.Checked)
			{
				return PointStyle.Line;
			}
			return PointStyle.Circle;
		}

		public Autodesk.Revit.DB.ElementType GetElementType()
		{
			Autodesk.Revit.DB.ElementType result = null;
			if (this.cbxFamily.SelectedItem != null)
			{
				Family family = (this.cbxFamily.SelectedItem as DbFamilyObject).LoadInSubTrans(this.mApp.ActiveUIDocument.Document);
				result = ElectricalUtils.GetLeaderType(this.mApp.ActiveUIDocument.Document, this.GetDimLinkStyle(), family);
				using (List<FamilySymbol>.Enumerator enumerator = family.GetSymbols().GetEnumerator())
				{
					if (enumerator.MoveNext())
					{
						result = enumerator.Current;
					}
				}
			}
			return result;
		}

		private void cbxFamily_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cbxFamily.SelectedItem != null)
			{
				this.pbxFamily.Image = (this.cbxFamily.SelectedItem as DbFamilyObject).GetImage();
			}
		}

		private void rbNullLeader_CheckedChanged(object sender, EventArgs e)
		{
			this.groupBox3.Enabled = !this.rbNullLeader.Checked;
		}

		private void rbLine_CheckedChanged(object sender, EventArgs e)
		{
		}

		private UIApplication mApp;

		private static DlgWireDim dll;
	}
}
